<?php

/**
 * 分页类
 * @copyright (c) fvzone.com All Rights Reserved.
 * @version 1.0
 * @author fvzone
 * $Id: page.php 2009-06-14 06:11:1 $
 */

/**
 * css分页样式
 *
.nav { clear:both; font-size:11px;height:24px; line-height:24px;text-align:left;margin:6px 0 12px 0px;}
.nav a,.nav a:link,.nav a:visited,.nav a:active{text-decoration:none;border:2px solid #2E3539;color:#fff;background:#3F464A;margin:2px;padding:2px 6px;}
.nav a:hover{border:2px solid #81AB20;color:#fff;background:#8dbc1f;}
.nav span.expand {line-height:24px;color:#999;border:2px solid #eee;background:#fff;margin:2px;padding:2px 6px;}
.nav span.current {font-weight:700;border:2px solid #81AB20;color:#fff;background:#8dbc1f;margin:2px;padding:2px 6px;}
*/

class pager {

    #每页显示条数
    public $number;
    #页码数
    public $few;
    #传入页码
    public $current;
    //分隔符
    public $split = 'page';
    #总页数
    public $items;
    #返回查询id
    public $begin;
    #基本URL地址
    public $baseurl;

	public function conf($total, $current, $url, $number = 6, $few = 2) 
	{
        $this->number = $number;
        $this->few = $few;
        //当前页码
	    $this->current = ($current == 0) ? 1 : intval($current);
        //指制分页URL地址
	    $this->baseurl = isset($url) ? $url : '';

        //总记录数小于1返回假
		if($total <= 0) return false;

		//总数据数除以每页显示的条数，有余进一
		$this->items = ceil($total/$this->number);

		//假如传入的页数参数大于总页数
		if($this->current > $this->items)
		{
            return false;
		}
    }

    //分页当前起始页
    public function offset()
    {
        return $this->offset = ($this->current -1) * $this->number;
    }

    public function getlink()
    {

        if(!$this->items) return false;

        $urls = explode($this->split, $this->baseurl);
    
        //偶数为双数,基数为单数, 每页显示数除以2,减去当前页码等于起始页码
        //round() -- 四舍五入, ceil() -- 进一法取整
        $start = ($this->current - round($this->few / 2)) > 0 ? ($this->current - round($this->few / 2)) : 1;
        $end = ($this->current + round($this->few / 2)) < $this->items ? ($this->current + round($this->few / 2)) : $this->items;
        $prestart = $start - 1;
        $nextend = $end + 1;
        $url = ' <a href="'.$urls[0].$this->split;

        $link = '<div id="pages">';
        $link .= '<a class="expand">Page '. $this->current .' of '. $this->items .'</a> ';

        #上一页和首页
        if($this->current <> 1)
            $link .= ''. $url.''.$urls[1].'" class="a1" title="首页">&laquo;First</a>';
        
        #如果开始页大于1显示上一页
        if($start > 1)
            $link .= '<span class="expand">...</span>'. $url.'='. $prestart.$urls[1].'" class="a1" title="上一页"> &laquo;</a>';
     
        #主循环
        for($i = $start; $i <= $end; $i++) {
            $link .= ($this->current == $i) ? ' <span class="current">'. $i .'</span>' : ''. $url.'='. $i.$urls[1].'" title="第'. $i .'页">'. $i .'</a>';
        }
        
        #下一页和尾页
        if($end < $this->items)
            $link .= ''. $url.'='. $nextend.$urls[1].'" class="a1" title="下一页">&raquo;</a> <span class="expand">...</span>';
        
        if($this->current <> $this->items)
            $link .= ''. $url.'='. $this->items.$urls[1].'" class="a1" title="尾页">Last&raquo;</a>';
        $link .= '</div>';

        return $link;
    }
}
?>
